<template>
  <div class="app-container">
    <div class="demo-image">
      <el-avatar v-for="img in imageList" shape="square" :size="100" fit="true" :src="img.fileUrl"></el-avatar>
    </div>
    <div id="app">
      <div class="upload">
        <input type="file" id="file" multiple @change="upload">
      </div>
      <ul class="view">
        <li v-for="(item,index) in fileList" :key="index">
          <img :src="item">
          <div class="delect" @click="delectFile(index)">×</div>
        </li>
      </ul>
    </div>
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <el-card class="box-card">
        <el-row>
          <el-col :span="1.5">
            <el-form-item label="车辆ID" prop="carId">
              <el-input v-model="form.carId" placeholder="请输入车辆ID"/>
            </el-form-item>
          </el-col>
          <el-col :span="1.5">
            <el-input v-model="form.carId" placeholder="请输入车辆ID"/>
          </el-col>
        </el-row>
      </el-card>
      <el-form-item label="车辆ID" prop="carId">
        <el-input v-model="form.carId" placeholder="请输入车辆ID"/>
      </el-form-item>
      <el-form-item label="删除标志" prop="delFlag">
        <el-input v-model="form.delFlag" placeholder="请输入删除标志"/>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"/>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <!--      <el-button @click="cancel">取 消</el-button>-->
    </div>
  </div>
</template>

<script>
import {addConfig, listConfig, updateConfig} from "@/api/car/config";
import {listFile, listUploadFile} from "@/api/car/car_file";

export default {
  name: "car_config_dl",
  props: ['carInfoId', 'busType'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 车辆-配置信息表格数据
      fileList: [],
      imageList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 表单参数
      form: {carId: this.carInfoId},
      // 表单校验
      rules: {
        carId: [
          {required: true, message: "车辆ID不能为空", trigger: "blur"}
        ],
        configType: [
          {required: true, message: "配置类型不能为空", trigger: "change"}
        ],
        status: [
          {required: true, message: "状态不能为空", trigger: "blur"}
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getInitData() {
      console.log('参数', this.carInfoId, this.busType);
      this.getList()
      // this.getFileList();
    },
    getList() {
      let params = {carId: this.carInfoId, configType: this.busType};
      listConfig({carId: this.carInfoId, configType: this.busType}).then(data => {
        console.log(data);
        if (data.total > 0) {
          this.form = data.rows[0] || params;
        } else {
          this.form = params;
        }
        this.loading = false;
      });
    }, getFileList() {
      this.loading = false;
      listFile({busType: this.busType, busId: this.carInfoId}).then(response => {
        console.log(response)
        this.imageList = response.rows;
        console.log(this.imageList)
        this.loading = false;
      });
    }, upload: function (e) {
      //e.target指向事件执行时鼠标所点击区域的那个元素，那么为input的标签，
      // 可以输出 e.target.files 看看，这个files对象保存着选中的所有的图片的信息。
      console.log(e.target.files)
      //------------------------------------------------------
      // 既然如此，循环这个files对象，用for of 循环，
      for (let item of e.target.files) {
        //正则表达式，判断每个元素的type属性是否为图片形式，如图
        if (!/image\/\w+/.test(item.type)) {
          // 提示只能是图片，return
          alert("只能选择图片");
          return;
        }
        // 保存下当前 this ，就是vue实例
        var _this = this;
        //  创建一个FileReader()对象，它里面有个readAsDataURL方法
        let reader = new FileReader();
        // readAsDataURL方法可以将上传的图片格式转为base64,然后在存入到图片路径,
        //这样就可以上传电脑任意位置的图片
        reader.readAsDataURL(item);
        //文件读取成功完成时触发
        reader.addEventListener('load', function () {
          //  reader.result返回文件的内容。
          //只有在读取操作完成后，此属性才有效，返回的数据的格式取决于是使用哪种读取方法来执行读取操作的。
          //给数组添加这个文件也就是图片的内容
          _this.fileList.push(this.result);
          // console.log(_this.list[0]);
          listUploadFile({'busType': _this.busType, busId: _this.carInfoId, 'base64Img': this.result, aaa: 1});
        });
      }
      //------------------------------------------------------------
    },
    // 取消按钮
    delectFile(index) {
      this.fileList.splice(index, 1);
    },
    // 表单重置
    reset() {
      this.form = {
        carConfigId: null,
        carId: null,
        configType: null,
        info: null,
        status: "0",
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.carConfigId != null) {
            updateConfig(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addConfig(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    }
  }
};
</script>

<style rel="stylesheet/scss" lang="scss">
#app {
  width: 900px;
  background-color: rgb(241, 241, 241);
  margin: 50px auto;
}

.view {
  display: flex;
  justify-content: space-around;
  flex-wrap: wrap;
  align-items: space-around;
}

.view > li {
  width: 200px;
  height: 120px;
  background-color: rgba(54, 194, 35, 0.1);
  list-style: none;
  margin: 20px;
  position: relative;
}

.view > li > img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.delect {
  position: absolute;
  right: 0;
  top: 0;
  width: 20px;
  height: 20px;
  text-align: center;
  line-height: 20px;
  font-size: 15px;
  background-color: rgba(255, 255, 255, 0.5);
  user-select: none;
  cursor: pointer;
  opacity: 0;
}

.delect:hover {

  background-color: rgba(31, 31, 31, 0.5);
  color: white;
}

.view > li:hover .delect {
  opacity: 1;
}

.upload {
  width: 80px;
  height: 20px;
  background-color: rgba(135, 206, 235, 0.2);
  border: 1px dashed black;
  border-radius: 5px;
  position: relative;

}

.upload:hover {
  background-color: rgba(135, 206, 235, 1);
}

.upload::before {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  content: '上传图片';
  font-size: 13px;
  text-align: center;
  font-family: 'fangsong';
  line-height: 20px;
  user-select: none;
}

#file {
  width: 100%;
  height: 100%;
  opacity: 0;
}
</style>
